package db

import (
	"github.com/gookit/color"
	"github.com/redis/go-redis/v9"
	"gorm.io/gorm"
	"log"
	"strconv"

	"utilx/dbx"
)

var (
	RedisMap = make(cachex.RedisBucket, 16)
	Mdb      *gorm.DB
)

func NewMysqlDB() *gorm.DB {
	return Mdb
}
func NewRedisMap() cachex.RedisBucket {
	return RedisMap
}
func InitDB(config map[string]dbx.DataBase) {
	for k, v := range config {
		switch k {
		case "postgres":
			Mdb = dbx.InitPostgres(v)
			color.Greenln(k, v)
		case "redis":
			for i := 0; i < 16; i++ {
				color.Greenln(k, v)
				v.DataBase = strconv.Itoa(i)
				rr, err := dbx.InitRedis(v)
				if err != nil {
					panic(err)
				}
				RedisMap[int32(i)] = rr
			}
		default:
			log.Println("database default", k, v)
		}
	}
}

func GetMysql(nu int) *gorm.DB {
	switch nu {
	case 0:
		return dbx.InitMysql(dbx.DataBase{
			Username: "root",
			Password: "root",
			Host:     "192.168.8.88:3307",
			DataBase: "PanelVod",
		})
	case 1:
		return dbx.InitMysql(dbx.DataBase{
			Username: "root",
			Password: "admin",
			Host:     "192.168.8.88:3307",
			DataBase: "stbcms",
		})

	default:
		panic("ss")
	}

	return nil
}
func GetRedis(nu int) (*redis.Client, error) {
	switch nu {
	case 0:
		return dbx.InitRedis(dbx.DataBase{
			Username: "",
			Password: "admin",
			Host:     "192.168.8.20:6379",
			DataBase: "0",
		})

	default:
		return dbx.InitRedis(dbx.DataBase{
			Username: "",
			Password: "admin",
			Host:     "192.168.8.20:6379",
			DataBase: "0",
		})
	}
}
